package com.px.user.controller;


import com.px.common.core.RespCode;
import com.px.common.core.Response;
import com.px.user.entity.Cheat;
import com.px.user.mapper.CheatMapper;
import com.px.user.model.ActionModel;
import com.px.user.model.UpTokenModel;
import com.px.user.service.CDNService;
import com.px.user.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@Slf4j
@RestController
@Api(value = "秘籍管理", tags = {"秘籍管理"})
@RequestMapping("/cheat")
public class CheatController {

    @Autowired
    private CDNService cdnService;

    @Autowired
    private CheatMapper cheatMapper;

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/upToken")
    @PreAuthorize("hasAnyAuthority('ADMIN', 'ROOMADMIN', 'TEACHER')")
    @ApiOperation(value = "上传token", authorizations = {@Authorization(value = "token")}, notes = "总管/讲师/房管使用", tags = {"秘籍"})
    public Response<UpTokenModel> upTokenGet() {
        return Response.success(cdnService.qiniuUpToken());
    }

    @PostMapping
    @PreAuthorize("hasAnyAuthority('ADMIN', 'ROOMADMIN', 'TEACHER')")
    @ApiOperation(value = "秘籍创建", authorizations = {@Authorization(value = "token")}, tags = {"秘籍"})
    public Response cheatPost(@Validated(ActionModel.Insert.class) @RequestBody Cheat cheat, Principal principal) {
        cheat.setCreatorId(Long.valueOf(principal.getName()));
        cheatMapper.insert(cheat);
        return Response.success(cheat.getId());
    }

    @GetMapping
    @ApiOperation(value = "秘籍列表", tags = {"秘籍"})
    public Response<List<Cheat>> cheatGet(
            @ApiParam(value = "讲师ID", example = "3") @RequestParam("id") Long id) {
        return Response.success(cheatMapper.findByCreatorId(id));
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('ADMIN', 'ROOMADMIN', 'TEACHER')")
    @ApiOperation(value = "秘籍更新", authorizations = {@Authorization(value = "token")}, notes = "总管/讲师/房管使用", tags = {"秘籍"})
    public Response cheatPut(@Validated(ActionModel.Update.class) @RequestBody Cheat cheat, Principal principal) {
        if (cheat.getId() == null)
            return Response.fail(RespCode.PARA_ERROR, null);
        cheat.setCreatorId(Long.valueOf(principal.getName()));
        cheatMapper.update(cheat);
        return Response.success(null);
    }


    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('TEACHER','ROOMADMIN','ADMIN')")
    @ApiOperation(value = "秘籍删除", authorizations = {@Authorization(value = "token")}, notes = "讲师/房管/总管使用", tags = {"秘籍"})
    public Response courseDelete(@ApiParam(value = "课程ID", example = "4")  @PathVariable("id") Integer id,
                                 Authentication authentication) {
        if (id == null)
            return Response.fail(RespCode.PARA_ERROR, null);
        teacherService.cheatDelete(id, authentication);
        return Response.success(null);
    }

}
